package com.niit.agricstore.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Cart;
import com.niit.agricstore.mapper.CartMapper;
import com.niit.agricstore.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    CartMapper cartMapper;

    @Override
    public List<Cart> findAllByCartId(Integer cartId) {
        return cartMapper.findAllByCartId(cartId);
    }

    @Override
    public List<Cart> findAllByCartChoose(String cartChoose) {
        return cartMapper.findAllByCartChoose(cartChoose);
    }

    @Override
    public int deleteByCartUid(long cartUid) {
        return cartMapper.deleteByCartUid(cartUid);
    }

    @Override
    public int deleteByCartProductId(int cartProductId) {
        return cartMapper.deleteByCartProductId(cartProductId);
    }

    @Override
    public int insertAll(Cart cart) {
        return cartMapper.insertAll(cart);
    }

    @Override
    public int insertSelective(Cart cart) {
        return cartMapper.insertSelective(cart);
    }

    @Override
    public int updateCartProductSaleAndCartPriceByCartProductId(int cartProductSale, int cartPrice, int cartProductId) {
        return cartMapper.updateCartProductSaleAndCartPriceByCartProductId(cartProductSale, cartPrice, cartProductId);
    }
    public Cart findAllByProductId(Integer pid,Long uid){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cart_product_id",pid);
        queryWrapper.eq("cart_uid",uid);
        return cartMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateCartChoose(String cartChoose,Long cartUid) {
        return cartMapper.updateCartChoose(cartChoose,cartUid);
    }

    @Override
    public int updateCartState(String cartState, int cartProductId ) {
        return cartMapper.updateCartState(cartState,cartProductId);
    }

    @Override
    public PageInfo<Cart> findCartAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Cart> cartList = cartMapper.selectList(queryWrapper); // 或者您自定义的查询方法
        return new PageInfo<>(cartList);
    }

    @Override
    public Page<Cart> getCartsByPage(int page, int pageSize) {
        Page<Cart> cartPage = new Page<>(page, pageSize);
        return cartMapper.selectPage(cartPage, null);
    }

    @Override
    public int updateCartProductSaleAndCartPriceByCartId(int cartProductSale, int cartPrice, int cartId) {

        return cartMapper.updateCartProductSaleAndCartPriceByCartId(cartProductSale, cartPrice, cartId);
    }

    @Override
    public int delByCartId(long cartId) {
        return cartMapper.delByCartId(cartId);
    }
    @Override
    public int addProduct(Cart cart) {
        return cartMapper.insert(cart);
    }

    @Override
    public List<Cart> findAllByUid(Long uid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cart_uid",uid);
        return cartMapper.selectList(queryWrapper);
    }

}
